﻿<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="Tutorial for interactivity with GoJS." />
  <title>GoJS Interactivity</title>
  <!-- Copyright 1998-2020 by Northwoods Software Corporation. -->
  <link href="../assets/css/bootstrap.min.css" rel="stylesheet" >
  <!-- custom CSS after bootstrap -->
  <link href="../assets/css/main.css" rel="stylesheet" type="text/css"/>
  <link href="../assets/css/highlight.css" rel="stylesheet" type="text/css" media="all" />
  <script src="../assets/js/highlight.js"></script>

  <script src="../release/go.js"></script>

  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-1506307-5', 'auto');
    ga('send', 'pageview');
  </script>
</head>

<body>
    <!-- non-fixed navbar -->
    <nav id="non-fixed-nav" class="navbar navbar-inverse navbar-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <div class="navheader-container">
            <div class="navheader-collapse" data-toggle="collapse" data-target="#navbar">
              <a id="toplogo" class="navbar-brand" href="../index.html">GoJS</a>
              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
            </div>
          </div>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="../index.html">Home</a></li>
            <li><a href="../learn/index.html">Learn</a></li>
            <li><a href="../samples/index.html">Samples</a></li>
            <li><a href="../intro/index.html">Intro</a></li>
            <li><a href="../api/index.html" target="api">API</a></li>
            <li><a href="https://www.nwoods.com/components/evalform.htm">Register</a></li>
            <li><a href="../download.html">Download</a></li>
            <li><a href="https://forum.nwoods.com/c/gojs">Forum</a></li>
            <li><a href="https://www.nwoods.com/contact.html" onclick="ga('send','event','Outbound Link','click','contact');">Contact</a></li>
            <li class="buy"><a href="https://www.nwoods.com/sales/index.html" onclick="ga('send','event','Outbound Link','click','buy');">Buy</a></li>
            <li class="activate"><a href="https://www.nwoods.com/app/activate.aspx?sku=gojs">Activate</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div id="bannertop" class="jumbotron banner">
      <div class="container-fluid plr15">
        <h1><span>GoJS Interactivity</span></h1>
      </div>
    </div>

    <div class="container-fluid learn-container">

      <h2 id="BuiltinGoJSInteractivity">Built-in GoJS Interactivity</h2>

      <p>
        <b>GoJS</b> implements a number of common editing operations, such as manipulating parts
        (moving, adding, copying, cutting, and deleting).
        These editing abilities are accessible via mouse (or touch) and keyboard by default,
        and can also be invoked programmatically in JavaScript.
      </p>

      <p>
        The following Diagram defines a node template and has four nodes in its model:
      </p>

      <pre class="lang-js">
var $ = go.GraphObject.make;
myDiagram =
  $(go.Diagram, "myDiagramDiv",
    { "undoManager.isEnabled": true });

// define a simple Node template
myDiagram.nodeTemplate =
  $(go.Node, "Auto",
    $(go.Shape, "Rectangle",
      // don't draw any outline
      { stroke: null },
      // the Shape.fill comes from the Node.data.color property
      new go.Binding("fill", "color")),
    $(go.TextBlock,
      // leave some space around larger-than-normal text
      { margin: 6, font: "18px sans-serif" },
      // the TextBlock.text comes from the Node.data.key property
      new go.Binding("text", "key"))
  );

myDiagram.model = new go.GraphLinksModel(
[
  { key: "Alpha", color: "lightblue" },
  { key: "Beta", color: "orange" },
  { key: "Gamma", color: "lightgreen" },
  { key: "Delta", color: "pink" }
]);
      </pre>

      <!-- LIVE -->
      <div id="myDiagramDiv" class="diagramStyling" style="width:700px; height:150px"></div>
      <script>
      function setupDiagram(divname) {
        var $ = go.GraphObject.make;
        var myDiagram =
          $(go.Diagram, divname,
            { "undoManager.isEnabled": true });

        myDiagram.nodeTemplate =
          $(go.Node, "Auto",
            $(go.Shape, "Rectangle",
              { stroke: null },
              new go.Binding("fill", "color")),
            $(go.TextBlock,
              { margin: 6, font: "18px sans-serif" },
              new go.Binding("text", "key"))
          );

        myDiagram.model = new go.GraphLinksModel(
        [
          { key: "Alpha", color: "lightblue" },
          { key: "Beta", color: "orange" },
          { key: "Gamma", color: "lightgreen" },
          { key: "Delta", color: "pink" }
        ]);
        return myDiagram;
      }
      setupDiagram("myDiagramDiv")
      </script>

      <p>
        Out of the box, several interactions are available:
      </p>
      <ul>
        <li>
          Click a node to select it.
          Press and drag on a node to move it around.
          Or press and drag in the diagram background to pan the entire Diagram.
        </li>
        <li>
          Common keyboard shortcuts such as <code>CTRL-C</code>, <code>CTRL-V</code>, <code>CTRL-X</code>
          will copy, paste, and cut diagram parts, respectively.
        </li>
        <li>
          Drag-and-hold allows you to create a selection box for selecting multiple nodes.
          CTRL-clicking on nodes allows you to toggle their selection.
        </li>
        <li>
          Since the Diagram's undoManager was enabled, <code>CTRL-Z</code> and <code>CTRL-Y</code> will undo and redo operations.
          Panning (scrolling) and selection are not considered undoable operations.
        </li>
      </ul>

      <p>
        By setting a few properties you can expose more interaction to a user:
      </p>

      <pre class="lang-js">
var $ = go.GraphObject.make;
var myDiagram =
  $(go.Diagram, divname,
    {
      // allow double-click in background to create a new node
      "clickCreatingTool.archetypeNodeData": { key: "Node", color: "white" },
      // allow Ctrl-G to group
      "commandHandler.archetypeGroupData": { text: "Group", isGroup: true },
      // have mouse wheel events zoom in and out instead of scroll up and down
      "toolManager.mouseWheelBehavior": go.ToolManager.WheelZoom,
      "undoManager.isEnabled": true // enable undo &amp; redo
    });

myDiagram.nodeTemplate =
  $(go.Node, "Auto",
    $(go.Shape, "Rectangle",
      { stroke: null },
      new go.Binding("fill", "color")),
    $(go.TextBlock,
      { margin: 6, font: "18px sans-serif", editable: true },
      new go.Binding("text", "key"))
  );

// allow CTRL-SHIFT-G to ungroup a selected Group
myDiagram.groupTemplate.ungroupable = true;

myDiagram.model = new go.GraphLinksModel(
[
  { key: "Alpha", color: "lightblue" },
  { key: "Beta", color: "orange" },
  { key: "Gamma", color: "lightgreen", group: "Group1" },
  { key: "Delta", color: "pink", group: "Group1" },
  { key: "Group1", isGroup: true }
]);
      </pre>

      <!-- LIVE -->
      <div id="myDiagramDiv2" class="diagramStyling" style="width:700px; height:150px"></div>
      <script>
      function setupDiagram(divname) {
        var $ = go.GraphObject.make;
        var myDiagram =
          $(go.Diagram, divname,
            {
              // allow double-click in background to create a new node
              "clickCreatingTool.archetypeNodeData": { key: "Node", color: "white" },
              // allow Ctrl-G to group
              "commandHandler.archetypeGroupData": { text: "Group", isGroup: true },
              // have mouse wheel events zoom in and out instead of scroll up and down
              "toolManager.mouseWheelBehavior": go.ToolManager.WheelZoom,
              "undoManager.isEnabled": true // enable undo & redo
            });

        myDiagram.nodeTemplate =
          $(go.Node, "Auto",
            $(go.Shape, "Rectangle",
              { stroke: null },
              new go.Binding("fill", "color")),
            $(go.TextBlock,
              { margin: 6, font: "18px sans-serif", editable: true },
              new go.Binding("text", "key"))
          );

        // allow CTRL-SHIFT-G to ungroup a selected Group
        myDiagram.groupTemplate.ungroupable = true;

        myDiagram.model = new go.GraphLinksModel(
        [
          { key: "Alpha", color: "lightblue" },
          { key: "Beta", color: "orange" },
          { key: "Gamma", color: "lightgreen", group: "Group1" },
          { key: "Delta", color: "pink", group: "Group1" },
          { key: "Group1", isGroup: true }
        ]);
        return myDiagram;
      }
      setupDiagram("myDiagramDiv2")
      </script>

      <p>
        <ul>
          <li>
            <code>ClickCreatingTool.archetypeNodeData</code> allows a double-click in the background to create
            a new node with the specified data.
          </li>
          <li>
            <code>CommandHandler.archetypeGroupData</code> allows <code>CTRL-G</code> to group a selection of nodes.
          </li>
          <li>
            <code>Group.ungroupable</code> allows <code>CTRL-SHIFT-G</code> to ungroup a selected group.
          </li>
          <li>
            <code>ToolManager.mouseWheelBehavior</code> allows the mouse wheel to zoom instead of scroll by default.
            You can toggle this property by clicking on the mouse-wheel.
            On touch devices, pinch-zooming is enabled by default.
          </li>
          <li>
            <code>TextBlock.editable</code> in the <code>TextBlock</code> definition allows the text to be edited in place.
            Select a node and then click on the text, or press F2, to begin text editing.
            Click anywhere else on the diagram or press <code>Tab</code> to finish editing text.
          </li>
        </ul>
      </p>

      <p>
        These interactions (and more!) are all present in the <a href="../samples/basic.html">basic.html sample</a>.
        Be sure to also see the <a href="../intro/commands.html">Intro page on GoJS commands</a>.
      </p>

      <p>
        You can disable portions of Diagram interactivity with several properties,
        depending on what you want to allow users to do.
        See the <a href="../intro/permissions.html">Intro page on GoJS permissions</a> for more.
      </p>

      <h2 id="ContextMenus">Context Menus</h2>

      <p>
        <b>GoJS</b> provides a mechanism for you to define context menus for any object or for the Diagram itself.
        In the example below, two context menus are defined, one on the Node template (with one button) and
        one on the Diagram (with two buttons).
      </p>

      <pre class="lang-js">
var $ = go.GraphObject.make;
var myDiagram =
  $(go.Diagram, divname,
    { "undoManager.isEnabled": true });

// this function is called when the context menu button is clicked
function nodeClicked(e, obj) {
  alert('node ' + obj.part.data.key + ' was clicked');
}

// defines a context menu to be referenced in the node template
var contextMenuTemplate =
  $(go.Adornment, "Vertical",
    $("ContextMenuButton",
      $(go.TextBlock, "Click me!"),
      { click: nodeClicked })
    // more ContextMenuButtons would go here
  );

myDiagram.nodeTemplate =
  $(go.Node, "Auto",
    { contextMenu: contextMenuTemplate }, // set the context menu
    $(go.Shape, "Rectangle",
      { stroke: null },
      new go.Binding("fill", "color")),
    $(go.TextBlock,
      { margin: 6, font: "18px sans-serif" },
      new go.Binding("text", "key"))
  );

// this function alerts the current number of nodes in the Diagram
function countNodes(e, obj) {
  alert('there are ' + e.diagram.nodes.count + ' nodes');
}

// this function creates a new node and inserts it at the last event's point
function addNode(e, obj) {
  var data = { key: "Node", color: "white" };
  e.diagram.model.addNodeData(data);
  var node = e.diagram.findPartForData(data);
  node.location = e.diagram.lastInput.documentPoint;
}

myDiagram.contextMenu =
  $(go.Adornment, "Vertical",
    $("ContextMenuButton",
      $(go.TextBlock, "Count Nodes"),
      { click: countNodes }),
    $("ContextMenuButton",
      $(go.TextBlock, "Add Node"),
      { click: addNode })
    // more ContextMenuButtons would go here
  );

myDiagram.model = new go.GraphLinksModel(
[
  { key: "Alpha", color: "lightblue" },
  { key: "Beta", color: "orange" }
]);
return myDiagram;
      </pre>

      <!-- LIVE -->
      <div id="myDiagramDiv3" class="diagramStyling" style="width:700px; height:150px"></div>
      <script>
      function setupDiagram(divname) {
        var $ = go.GraphObject.make;
        var myDiagram =
          $(go.Diagram, divname,
            { "undoManager.isEnabled": true });

        // this function is called when the context menu button is clicked
        function nodeClicked(e, obj) {
          alert('node ' + obj.part.data.key + ' was clicked');
        }

        // defines a context menu to be referenced in the node template
        var contextMenuTemplate =
          $(go.Adornment, "Vertical",
            $("ContextMenuButton",
              $(go.TextBlock, "Click me!"),
              { click: nodeClicked })
            // more ContextMenuButtons would go here
          );

        myDiagram.nodeTemplate =
          $(go.Node, "Auto",
            { contextMenu: contextMenuTemplate }, // set the context menu
            $(go.Shape, "Rectangle",
              { stroke: null },
              new go.Binding("fill", "color")),
            $(go.TextBlock,
              { margin: 6, font: "18px sans-serif" },
              new go.Binding("text", "key"))
          );

        // this function alerts the current number of nodes in the Diagram
        function countNodes(e, obj) {
          alert('there are ' + e.diagram.nodes.count + ' nodes');
        }

        // this function creates a new node and inserts it at the last event's point
        function addNode(e, obj) {
          var data = { key: "Node", color: "white" };
          e.diagram.model.addNodeData(data);
          var node = e.diagram.findPartForData(data);
          node.location = e.diagram.lastInput.documentPoint;
        }

        myDiagram.contextMenu =
          $(go.Adornment, "Vertical",
            $("ContextMenuButton",
              $(go.TextBlock, "Count Nodes"),
              { click: countNodes }),
            $("ContextMenuButton",
              $(go.TextBlock, "Add Node"),
              { click: addNode })
            // more ContextMenuButtons would go here
          );

        myDiagram.model = new go.GraphLinksModel(
        [
          { key: "Alpha", color: "lightblue" },
          { key: "Beta", color: "orange" }
        ]);
        return myDiagram;
      }
      setupDiagram("myDiagramDiv3")
      </script>

      <p>
        If you right-click (or long-tap on a touch device) on a Node or the Diagram, you will see the <b>GoJS</b> context menu
        appear with the defined options.
      </p>

      <p>
        The <a href="../samples/basic.html">basic.html sample</a> contains more complex context menu examples.
        See the <a href="../intro/contextmenus.html">Intro page on GoJS context menus</a> for more.
      </p>

      <h2 id="LinkInteractions">Link interactions</h2>

      <p>
        <b>GoJS</b> lets users draw new links and re-link existing links by dragging out from a port or
        selecting a link and dragging its handles.
        To enable these tools, some properties need to be set:
      </p>

      <pre class="lang-js">
myDiagram.nodeTemplate =
  $(go.Node, "Auto",
    $(go.Shape, "Rectangle",
      {
        stroke: null,
        portId: "",
        cursor: "pointer",
        fromLinkable: true, fromLinkableSelfNode: true, fromLinkableDuplicates: true,
        toLinkable: true, toLinkableSelfNode: true, toLinkableDuplicates: true
       },
      new go.Binding("fill", "color")),
    $(go.TextBlock,
      { margin: 6, font: "18px sans-serif" },
      new go.Binding("text", "key"))
  );

myDiagram.linkTemplate =
  $(go.Link,
    {
      // allow the user to relink existing links:
      relinkableFrom: true, relinkableTo: true,
      // draw the link path shorter than normal,
      // so that it does not interfere with the appearance of the arrowhead
      toShortLength: 2
    },
    $(go.Shape,
      { strokeWidth: 2 }),
    $(go.Shape,
      { toArrow: "Standard", stroke: null })
  );

myDiagram.model = new go.GraphLinksModel(
[
  { key: "Alpha", color: "lightblue" },
  { key: "Beta", color: "orange" },
  { key: "Gamma", color: "lightgreen" },
  { key: "Delta", color: "pink" }
],
[
  { from: 'Alpha', to: 'Beta' },
  { from: 'Alpha', to: 'Delta' },
]
);
      </pre>

      <!-- LIVE -->
      <div id="myDiagramDiv4" class="diagramStyling" style="width:700px; height:150px"></div>
      <script>
      function setupDiagram(divname) {
        var $ = go.GraphObject.make;

        var myDiagram =
          $(go.Diagram, divname,
            { "undoManager.isEnabled": true });

        myDiagram.nodeTemplate =
          $(go.Node, "Auto",
            $(go.Shape, "Rectangle",
              {
                stroke: null,
                portId: "",
                cursor: "pointer",
                fromLinkable: true, fromLinkableSelfNode: true, fromLinkableDuplicates: true,
                toLinkable: true, toLinkableSelfNode: true, toLinkableDuplicates: true
              },
              new go.Binding("fill", "color")),
            $(go.TextBlock,
              { margin: 10, font: "18px sans-serif" },
              new go.Binding("text", "key"))
          );

        myDiagram.linkTemplate =
          $(go.Link,
            {
              // allow the user to relink existing links:
              relinkableFrom: true, relinkableTo: true,
              // draw the link path shorter than normal,
              // so that it does not interfere with the appearance of the arrowhead
              toShortLength: 2
            },
            $(go.Shape,
              { strokeWidth: 2 }),
            $(go.Shape,
              { toArrow: "Standard", stroke: null })
          );

        myDiagram.model = new go.GraphLinksModel(
        [
          { key: "Alpha", color: "lightblue" },
          { key: "Beta", color: "orange" },
          { key: "Gamma", color: "lightgreen" },
          { key: "Delta", color: "pink" }
        ],
        [
          { from: 'Alpha', to: 'Beta' },
          { from: 'Alpha', to: 'Delta' },
        ]);
        return myDiagram;
      }
      setupDiagram("myDiagramDiv4")
      </script>

      <p>
        In the above example:
      </p>
      <ul>
        <li>
          The <code>Shape</code> has its <code>portId</code> set to make it the port instead of the entire node.
          Then several <code>...linkable</code> properties are set, allowing each node to link to itself and others.
        </li>
        <li>
          To create a new link to another node, drag from the port (the edge of the Shape that is not
          behind the TextBlock) to any node, including itself.
        </li>
        <li>
          In the link template, <code>relinkable...</code> properties are set, so that when the link is selected,
          it shows handles that can be dragged in order to reconnect the link with a different node.
        </li>
      </ul>

      <p>
        <b>GoJS</b> allows linking and re-linking to abide by custom criteria in which source and destination nodes are valid.
        You can read about this in the <a href="../intro/validation.html">Intro page on Validation</a>.
      </p>

      <p>
        <b>GoJS</b> links have many interesting properties that are covered in depth in the
        <a href="../intro/links.html">Intro page on Links</a> and on the following pages.
      </p>

      <p>
        You may want to read more tutorials, such as the <a href="index.html">Learn GoJS</a> tutorial
        and the <a href="graphObject.html">GraphObject Manipulation</a> tutorial
        You can also watch tutorials on <a href="https://www.youtube.com/channel/UC9We8EoX596-6XFjJDtZIDg">YouTube</a>.
      </p>

      <p class="footer">
        GoJS &reg; by Northwoods Software. Copyright &copy; 1998-2020 <a href="https://www.nwoods.com" target="_blank">Northwoods Software</a> &reg;
      </p>
  </div> <!-- end main -->
  <div class="banner" id="bannerbottom">
    <!-- text in banner-->
  </div>
  <script src="../assets/js/jquery.min.js"></script>
  <script async src="../assets/js/bootstrap.min.js"></script>
</body>
</html>
